package com.winter.service;

import com.google.common.base.Strings;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winter.mapper.DictEntryMapper;
import com.winter.mapper.FinBondMapper;
import com.winter.mapper.FinContentLikeInfoMapper;
import com.winter.mapper.FinFavInfoMapper;
import com.winter.mapper.IcompanyCardMapper;
import com.winter.mapper.UserInfoMapper;
import com.winter.model.DictEntry;
import com.winter.model.FinBond;
import com.winter.model.FinContentLikeInfo;
import com.winter.model.IcompanyCard;
import com.winter.model.UserInfo;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

@Service
@Transactional
public class FinBondService {

    @Autowired
    public FinBondMapper finBondMapper;

    @Autowired
    private DictEntryMapper dictEntryMapper;

    @Autowired
    private FinFavInfoMapper finFavInfoMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private IcompanyCardMapper icompanyCardMapper;
    /**
     * 点赞
     */
    @Autowired
    private FinContentLikeInfoMapper finContentLikeInfoMapper;

    /**
     * 查询报价平台债券列表
     */
    public Result queryPageList(Integer pageNumber, Integer pageSize, String direct, String bond_type, String deadline, String valuation, String userNo, String sortName, String sort, String key) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }

        PageHelper.startPage(pageNumber, pageSize, false);
        List<FinBond> finCpaperList = finBondMapper.queryPageList(direct, bond_type, deadline, valuation, sortName, sort, key);
        finCpaperList.forEach(finBond -> {
                    String photoPath = null;
                    String photoType = finBond.getPhotoType();
                    if (!Strings.isNullOrEmpty(photoType)) {
                        if (photoType.equals("0")) {
                            // 获取创建人头像
                            String createUser = finBond.getCreateUser();
                            UserInfo userInfo = userInfoMapper.selectByUserNo(createUser);
                            if (userInfo != null) {
                                String custImgUrl = userInfo.getCustImgUrl();
                                if (!Strings.isNullOrEmpty(custImgUrl)) {
                                    finBond.setAvatar(userInfo.getCustImgUrl());
                                }
                            } else {
                                //(数据类型)(最小值+Math.random()*(最大值-最小值+1))
                                //(int)(1+Math.random()*(10-1+1))
                                int verycode = (int) (1 + Math.random() * (70 - 1 + 1));
                                photoPath = "/p2p/images/imgthumbs/headimg_" + verycode + ".jpg";
                                finBond.setAvatar(photoPath);
                            }
                        } else {
                            //(数据类型)(最小值+Math.random()*(最大值-最小值+1))
                            //(int)(1+Math.random()*(10-1+1))
                            int verycode = (int) (1 + Math.random() * (70 - 1 + 1));
                            photoPath = "/p2p/images/imgthumbs/headimg_" + verycode + ".jpg";
                            finBond.setAvatar(photoPath);
                        }
                    }
                    finBond.setFinFavStatus("0");
                    finBond.setIsLike("0");
                    getTypeName(finBond);
                    if (!Strings.isNullOrEmpty(userNo)) {
                        // 收藏
                        Long aLong = finFavInfoMapper.get(finBond.getId(), "1", 6, userNo);
                        if (aLong > 0) {
                            finBond.setFinFavStatus("1");
                        }
                        // 点赞
                        List<FinContentLikeInfo> isLike = finContentLikeInfoMapper.getIsLike(finBond.getId(), "1", "6", userNo);
                        if (isLike.size() > 0) {
                            finBond.setIsLike("1");
                        }
                    }
                }
        );
        PageInfo<FinBond> pageInfo = new PageInfo<>(finCpaperList);
        return Result.success(pageInfo);
    }

    public void getTypeName(FinBond finBond) {
        DictEntry dictEntry = dictEntryMapper.get("DC_BOND_DIRECTION", finBond.getDirect());
        if (dictEntry != null) {
            finBond.setDirect(dictEntry.getDictname());
        }
        DictEntry dictEntry1 = dictEntryMapper.get("DC_BOND_TYPE", finBond.getBondType());
        if (dictEntry1 != null) {
            finBond.setBondType(dictEntry1.getDictname());
        }
    }

    /**
     * 通过id查询报价平台债券
     */
    public Result getFinBondId(Long id, String userNo) {
        FinBond fb = finBondMapper.selectByPrimaryKey(id);
        // 获取字典名称
        getTypeName(fb);
        fb.setFinFavStatus("0");
        fb.setIsLike("0");
        if (!Strings.isNullOrEmpty(userNo)) {
            // 6  = 代表债券 获取用户是否收藏
            Long count = finFavInfoMapper.get(id, "1", 6, userNo);
            if (count > 0) {
                fb.setFinFavStatus("1");
            }
            // 点赞
            List<FinContentLikeInfo> isLike = finContentLikeInfoMapper.getIsLike(fb.getId(), "1", "6", userNo);
            if (isLike.size() > 0) {
                fb.setIsLike("1");
            }
        }
        return Result.success(fb);
    }

    /**
     * 新增报价平台债券信息
     */
    public Result insertFinBond(FinBond fb) {
        if (Strings.isNullOrEmpty(fb.getCreateUser())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (Strings.isNullOrEmpty(fb.getDirect())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请选择方向");
        }
        if (Strings.isNullOrEmpty(fb.getBondType())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请选择券种");
        }
        if (Strings.isNullOrEmpty(fb.getMonEnd())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请输入金额");
        }
        if (Strings.isNullOrEmpty(fb.getContact())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请输入联系人");
        }
        if (!Strings.isNullOrEmpty(fb.getValBeg()) && !Strings.isNullOrEmpty(fb.getValEnd())) {
            if (Integer.valueOf(fb.getValBeg()) > Integer.valueOf(fb.getValEnd())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "估值开始不能大于估值结束");
            }
        }


        UserInfo userInfo = userInfoMapper.selectByUserNo(fb.getCreateUser());
        String orgNo = userInfo.getOrgNo();
        IcompanyCard icompanyCard = icompanyCardMapper.selectByPrimaryKey(Long.valueOf(orgNo));
        // 公司信息
        fb.setUserName(userInfo.getUserName());
        fb.setUserCom(icompanyCard.getCompanyName());
        fb.setUserDepart(userInfo.getDepart());
        fb.setUserPost(userInfo.getPost());
        fb.setUserEmail(userInfo.getOrgEmail());
        fb.setUserQq(userInfo.getQq() + "");
        fb.setUserWx(userInfo.getWxNo());
        fb.setTopTime(new Date());
        fb.setInfoSrc("1");
        // 组装数据
        fb.setCreatetime(new Date());
        fb.setUpdatetime(new Date());
        fb.setPhotoType("0");
        fb.setStas("3");
        fb.setIsvalid("1");
        fb.setAuditing("0");
        fb.setCommentCount(new BigDecimal("0"));
        fb.setFavCount(new BigDecimal("0"));
        fb.setLikeCount(new BigDecimal("0"));
        fb.setCommentFlag("1");
        fb.setLikeFlag("1");
        fb.setReplayFlag("1");
        fb.setUpdateUser(fb.getCreateUser());
        finBondMapper.insertSelective(fb);
        return Result.success();
    }

    /**
     * 修改债券信息
     */
    public Result updateFinBond(FinBond fb) {
        if (Strings.isNullOrEmpty(fb.getCreateUser())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (fb.getId() == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "主键id不能为空");
        }
        if (Strings.isNullOrEmpty(fb.getDirect())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请选择方向");
        }
        if (Strings.isNullOrEmpty(fb.getBondType())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请选择券种");
        }
        if (Strings.isNullOrEmpty(fb.getMonEnd())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请输入金额");
        }
        if (Strings.isNullOrEmpty(fb.getContact())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "请输入联系人");
        }
        if (!Strings.isNullOrEmpty(fb.getValBeg()) && !Strings.isNullOrEmpty(fb.getValEnd())) {
            if (Integer.valueOf(fb.getValBeg()) > Integer.valueOf(fb.getValEnd())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "估值开始不能大于估值结束");
            }
        }
        fb.setUpdatetime(new Date());
        finBondMapper.updateByPrimaryKeySelective(fb);
        return Result.success("修改成功");
    }
}
